<template>
  <el-dialog
    v-el-drag-dialog
    title="催单"
    :visible.sync="visible"
    width="600px"
    @closed="reset"
  >
    <el-form ref="dataForm" :model="temp">
      <el-form-item>
        <el-radio-group v-model="radio" @change="radioChange">
          <el-radio v-for="item in urgeList" :key="item.keyId" :label="item.keyId">{{ item.keyName }}</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item prop="remark">
        <el-input v-model="temp.remark" type="textarea" :rows="3" maxlength="255" show-word-limit />
      </el-form-item>
      <el-timeline :reverse="false" style="margin-top: 30px;">
        <el-timeline-item
          v-for="(activity, index) in activities"
          :key="index"
          :timestamp="moment(activity.happentDt).format('YYYY-MM-DD HH:mm')"
        >
          {{ activity.regUser }}&emsp;催单一次&emsp;{{ `理由：${activity.remark}` }}&emsp;{{ activity.comeFrom === 1 ? '手动催单' : '系统催单' }}
        </el-timeline-item>
      </el-timeline>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="visible = false">
        取消
      </el-button>
      <el-button :loading="$loadingPlugin.submit" type="primary" @click="success">
        确定
      </el-button>
    </div>
  </el-dialog>
</template>

<script>
  import { queryOrderPress, queryUrgeRecordList, urgeOrderList } from '@/api'
  import moment from 'dayjs'

  export default {
    name: 'press-order',
    data() {
      return {
        moment,
        visible: false,
        billCode: '',
        billType: 1,
        radio: '',
        temp: {
          remark: ''
        },
        activities: [],
        text: '送达',
        urgeList: []
      }
    },
    methods: {
      open(temp) {
        this.getUrgeOrderList()
        this.getUrgeRecordList(temp)
        return new Promise(resolve => {
          this.resolve = resolve
          this.visible = true
          if (temp) {
            if (temp.billCode) this.billCode = temp.billCode
            if (temp.billType) this.billType = temp.billType
            if (temp.text) this.text = temp.text
          }
        })
      },
      // 催单字典
      async getUrgeOrderList() {
        const res = await urgeOrderList()
        if (res && res.data) {
          this.urgeList = res.data
        }
      },
      // 查询催单记录
      async getUrgeRecordList(temp) {
        const res = await queryUrgeRecordList({
          billCode: temp.billCode,
          billType: temp.billType
        })
        this.activities = JSON.parse(res.data.urgebillList)
      },
      radioChange(value) {
        const currentitem = this.urgeList.find(item => item.keyId === value)
        if (currentitem && currentitem.keyName) {
          this.temp.remark = currentitem.keyName
        }
      },
      success() {
        this.$refs.dataForm.validate(valid => {
          if (!valid) return
          const params = { billCode: this.billCode, ...this.temp }
          this.submit(params)
        })
      },
      async submit(params) {
        let url = ''
        switch (this.billType) {
        case 1:url = '/proxy/order/orderbill/urge'; break
        case 2:url = '/proxy/order/giveback/urge'; break
        case 3:url = '/proxy/order/repair/urge'; break
        case 4:url = '/proxy/order/complain/urge'; params.complainId = params.billCode; break
        }
        await queryOrderPress({
          url,
          params
        })
        this.resolve(true)
        this.visible = false
        this.$notify({
          title: '提示',
          message: '催单成功!',
          type: 'success',
          duration: 2000
        })
      },
      reset() {
        this.temp = {
          remark: ''
        }
        this.radio = ''
        this.activities = []
        this.text = '送达'
        this.$nextTick(() => {
          this.$refs.dataForm.clearValidate()
        })
      }
    }
  }
</script>

<style scoped>

</style>
